//<<<+++OPENSOURCE
//<<<+++OPENSOURCE_LICENSE
/**
 * @addtogroup FXCODEC
 * @{
 */

//<<<+++OPENSOURCE_MUST_BEGIN
/**
 * @file
 * @brief Various external codec provider
 */
#ifndef _FX_CODEC_PROVIDER_H_
#define _FX_CODEC_PROVIDER_H_
//<<<+++OPENSOURCE_MUST_END

#ifndef _FXM_OPENSOURCE_
//<<<+++OPENSOURCE_BEGIN LIC==FOXIT
class CFX_DIBAttribute;
//<<<+++OPENSOURCE_END
#endif

/**@brief External Jpeg codec provider */
class IFX_JpegProvider
{
public:
	/** Release the Jpeg codec provider. */
	virtual void		Release() = 0;

	/**
	 * Create a scanline decoder. 
	 *
	 * @param[in] src_buf			The input source JPEG-encoded buffer.
	 * @param[in] src_size			The size in bytes of the buffer.
	 * @param[in] width				The width of the image in pixels.
	 * @param[in] height			The height of the image in scanlines.
	 * @param[in] nComps			The number of components.
	 * @param[in] ColorTransfrom	Whether need to transform color
	 * @return A scanline decoder context pointer
	 */
	virtual void*		CreateDecoder(FX_LPCBYTE src_buf, FX_DWORD src_size, int width, int height, int nComps, FX_BOOL ColorTransform) = 0;
	
	/**
	 * Destroy the scanline decoder. 
	 *
	 * @param[in] pDecoder		The scanline decoder context pointer.
	 */
	virtual void		DestroyDecoder(void* pDecoder) = 0;

	/**
	 * Set the downscale parameters.
	 *
	 * @param[in] pDecoder		TThe scanline decoder context pointer.
	 * @param[in] dest_width	The destination width.
	 * @param[in] dest_height	The destination height.
	 */
	virtual void		DownScale(void* pDecoder, int dest_width, int dest_height) = 0;

	/** 
	 * Rewind the scanline decoder. 
	 *
	 * @param[in] pDecoder		The scanline decoder context pointer.
	 */
	virtual FX_BOOL		Rewind(void* pDecoder) = 0;

	/** 
	 * Get next scanline. 
	 *
	 * @param[in] pDecoder		The scanline decoder context pointer.
	 */
	virtual FX_LPBYTE	GetNextLine(void* pDecoder) = 0;

	/** 
	 * Get the original offset in the source buffer. 
	 *
	 * @param[in] pDecoder		The scanline decoder context pointer.
	 */
	virtual FX_DWORD	GetSrcOffset(void* pDecoder) = 0;
	
	/**
	 * Load information from a JPEG-encoded buffer.
	 *
	 * @param[in] src_buf					The input source JPEG-encoded buffer.
	 * @param[in] src_size					The size in bytes of the buffer.
	 * @param[out] width					It receives the image width.
	 * @param[out] height					It receives the image height.
	 * @param[out] num_components			It receives the number of components.
	 * @param[out] bits_per_components		It receives bits per component.
	 * @param[out] color_transform			It receives whether need to transform color.
	 * @param[out] icc_buf_ptr				It receives the icc profile data if it exist. The caller should FX_Free it after use it.
	 * @param[out] icc_length				It receives the icc profile stream size if it exist.
	 * @param[Out] pAttribute			The attribute information of bitmap.
	 * @return Non-zero means success, otherwise failure.
	 */
#ifdef _FXM_OPENSOURCE_
	//<<<+++OPENSOURCE_BEGIN LIC==GOOGLE
	virtual FX_BOOL		LoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
								int& num_components, int& bits_per_components, FX_BOOL& color_transform,
								FX_LPBYTE* icc_buf_ptr = NULL, FX_DWORD* icc_length = NULL) = 0;
	//<<<+++OPENSOURCE_END
#else
	//<<<+++OPENSOURCE_BEGIN LIC==FOXIT
	virtual FX_BOOL		LoadInfo(FX_LPCBYTE src_buf, FX_DWORD src_size, int& width, int& height,
								int& num_components, int& bits_per_components, FX_BOOL& color_transform,
								FX_LPBYTE* icc_buf_ptr = NULL, FX_DWORD* icc_length = NULL, CFX_DIBAttribute* pAttribute = NULL) = 0;
	//<<<+++OPENSOURCE_END
#endif

	/**
	 * Encode an image. 
	 * If the encoded CMYK stream be embedded to PDF, the application must 
	 * set an image dictionaries option entry, /Decode [1 0 1 0 1 0 1 0], first. 
	 *
	 * @param[in] pSource				The source image to be encoded.
	 * @param[out] dest_buf				It receives the encoded JPEG data.
	 * @param[out] dest_size			It receives the size of the encoded JPEG data. The caller must release the data by FX_Free.
	 * @param[in]  quality				The compressed quality of destination jpeg image(0~100)	
	 * @param[in] icc_buf				The icc profile will embeded to the compressed stream.
	 * @param[in] icc_length			The icc profile stream size.
	 * @param[in] pAttribute			The attribute information of bitmap.
	 * @return Non-zero means success, otherwise failure.
	 */
#ifdef _FXM_OPENSOURCE_
	//<<<+++OPENSOURCE_BEGIN LIC==GOOGLE
	virtual FX_BOOL		Encode(const class CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality = 75,
								FX_LPCBYTE icc_buf = NULL, FX_DWORD icc_length = 0) = 0;
	//<<<+++OPENSOURCE_END
#else
	//<<<+++OPENSOURCE_BEGIN LIC==FOXIT
	virtual FX_BOOL		Encode(const class CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality = 75,
								FX_LPCBYTE icc_buf = NULL, FX_DWORD icc_length = 0, CFX_DIBAttribute* pAttribute = NULL) = 0;
	//<<<+++OPENSOURCE_END
#endif

	/** 
	 * Start a progressive JPEG decoding process.
	 *
	 * @return A context pointer.
	 */
	virtual void*		Start() = 0;

	/** 
	 * Finish a progressive JPEG decoding process.
	 *
	 * @param[in] pContext				The context pointer.
	 */
	virtual void		Finish(void* pContext) = 0;

	/** 
	 * Provide input to the decoder. This function can be called multiple times, everytime with some new input.
	 *
	 * @param[in] pContext				The context pointer.
	 * @param[in] src_buf				Input buffer.
	 * @param[in] src_size				Size of input data.
	 */
	virtual void		Input(void* pContext, FX_LPCBYTE src_buf, FX_DWORD src_size) = 0;

	/** 
	 * Reader the header of the JPEG stream so we can get basic information about the picture.
	 *
	 * @param[in] pContext				The context pointer.
	 * @param[out] width				Receiving image width.
	 * @param[out] height				Receiving image height.
	 * @param[out] nComps				Receiving number of components.
	 * @param[out] pAttribute		The attribute information of bitmap.
	 * @return 0 for success, 1 for error, 2 for suspended (need more input).
	 */
#ifdef _FXM_OPENSOURCE_
	//<<<+++OPENSOURCE_BEGIN LIC==GOOGLE
	virtual int			ReadHeader(void* pContext, int* width, int* height, int* nComps) = 0;
	//<<<+++OPENSOURCE_END
#else
	//<<<+++OPENSOURCE_BEGIN LIC==FOXIT
	virtual int			ReadHeader(void* pContext, int* width, int* height, int* nComps, CFX_DIBAttribute* pAttribute = NULL) = 0;
	//<<<+++OPENSOURCE_END
#endif
	
	/** 
	 * Start scanline decoding, and specify down scale. ReadHeader() must be successful before this function is called.
	 *
	 * @param[in] pContext				The context pointer.
	 * @param[in] down_scale			Down scale factore, can be 1 (no down scale), 2, 4, or 8.
	 *									If the picture is very big, then down scaling can be very efficient.
	 *									After down-scaling, output width and height will be reduced:
	 *									output_width = (original_width + down_scale - 1) / down_scale
	 *									output_height = (original_height + down_scale - 1) / down_scale
	 * @return TRUE for success, FALSE for error or suspended (need more data)
	 */
	virtual int			StartScanline(void* pContext, int down_scale) = 0;
	
	/** 
	 * Read one scanline from the decoder. StartScanline() must be called first.
	 *
	 * @param[in] pContext				The context pointer.
	 * @param[in] dest_buf				Pointer to the scanline buffer. The caller must ensure the buffer 
	 *									is big enough to hold the whole scanline.
	 * @return TRUE for success, FALSE for error or suspended (need more data)
	 */
	virtual FX_BOOL		ReadScanline(void* pContext, FX_LPBYTE dest_buf) = 0;
	
	/** 
	 * Get number of bytes available in the input buffer.
	 *
	 * @param[in] pContext				The context pointer.
	 * @param[out] avail_buf_ptr		It receives the avail buf pointer, 
	 *									do not free it, it's partial src_buf of the caller Input.
	 * @return Number of bytes left in the input buffer.
	 */
	virtual FX_DWORD	GetAvailInput(void* pContext, FX_LPBYTE* avail_buf_ptr = NULL) = 0;
};

//<<<+++OPENSOURCE_MUST_BEGIN
#endif	// _FX_CODEC_PROVIDER_H_
//<<<+++OPENSOURCE_MUST_END

/** @} */

